package com.k2data.k2app.service;

import com.k2data.k2app.domain.SysMail;
import com.k2data.k2app.mapper.SysMailMapper;
import com.k2data.k2app.utils.Encryption3DES;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lidong 17-3-6.
 */
@Log4j2
@Service
public class SysMailService {

    private final SysMailMapper sysMailMapper;

    @Autowired
    public SysMailService(SysMailMapper sysMailMapper) {
        this.sysMailMapper = sysMailMapper;
    }

    @Transactional
    public int insert(SysMail sysMail){
        sysMail.setEmailPassword(getPassword3DES(sysMail.getEmailPassword()));
        int num = sysMailMapper.insertSelective(sysMail);
        return num;
    }

    public int update(SysMail sysMail){
        sysMail.setEmailPassword(getPassword3DES(sysMail.getEmailPassword()));
        int num = sysMailMapper.updateByPrimaryKeySelective(sysMail);
        return num;
    }

    public String getPassword3DES(String old){
        Encryption3DES encryption3DES = new Encryption3DES();
        String reValue = encryption3DES.get3DESEncrypt(old,"k2data");
        reValue = reValue.trim().intern();
        return reValue;
    }

    public SysMail get(Long id){
        SysMail sysMail = sysMailMapper.get(id);
        sysMail.setEmailPassword(getPassword3DES(sysMail.getEmailPassword()));
        return sysMail;
    }

}
